
/**
 * 在字符串中查找字符 ab
 * 
 * @param {*} source 
 * @returns 返回第一个匹配的位置
 */
function findAB(source) {
    let  findIndex = -1;
    if(source != null && source.length > 1) {
        for(let index in source) {
            let c = source.charAt(index);
            let nextIndex = parseInt(index) + 1;
            // JavaScript 加 +1 默认是字符串相加, 需要使用 parseInt
            // console.log(" index:" + index + " c:" + c + " nextIndex:" + nextIndex);
            if(nextIndex <= source.length) {
                let nextChar = source.charAt(nextIndex);
                // console.log(" nextChar:" + nextChar);
                if(c == "a" && nextChar == "b") {
                    findIndex = index;
                    break;
                }
            }
        }
    }

    return findIndex;
}

let result1 = findAB("abcde");
console.log(" result1:" + result1);
let result2 = findAB("demoab");
console.log(" result2:" + result2);
let result3 = findAB("demoa");
console.log(" result3:" + result3);

